<?php
/*
 * Last Edited: 27-7-2013
 */
class CouponModel extends Zend_Db_Table_Abstract{
	protected $_name = 'coupon';
	protected $_primary = 'id';
    protected $_itemPerPage = 5;
	
    public function getList($params = NULL){

		$db = Zend_Registry::get('connectDB');
        $mysql = $db->select()
					->from($this->_name);
        if(!empty($params['user_name'])){
            $mysql->where("user_receive_name = ?", $params['user_name']);
        }
        if(!empty($params['game_id'])){
            $mysql->where("game_id = ?", $params['game_id']);
        }
        $mysql->where("status = 1");
        $list = $db->fetchAll($mysql);
        return $list;
	}

    public function multiDelete($params,$options=NULL){
		if (!empty($params))	
			foreach ($params as $key => $id) {
				$where = array("id = ".intval($id));
				if ($id != 0) {
					self::delete($where);	
				}
			}
    }
    
    public function getItem($params=NULL,$options=NULL){
		if (isset($params['id'])){			
			$db = Zend_Registry::get('connectDB');
			$mySql = $db->select()
						->from($this->_name)
						->where('id = ?', $params['id']);
			return $db->fetchRow($mySql);	
		}
		return false;
	}
    
    public function getCoupon($params=NULL){	
		$db = Zend_Registry::get('connectDB');
		$mySql = $db->select()
					->from($this->_name)
                    ->where('value = ?', $params['coupon_value'])
                    ->where('status = 1')
                    ->where('receive_user is NOT NULL')
                    ->where("NOW() BETWEEN start_time AND end_time");
		return $db->fetchRow($mySql);	
	}
	public function validateCoupon($coupon){
		$query = $this->db->select()
		->from("coupon",array("id"))
		->where("value = ?", $coupon)
		->where("end_time > ?", new Zend_Db_Expr("NOW()"));
		$result = $query->query()->fetchAll();
		return empty($result);
	}
}